package com.IO.Demo;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class D01 {
    public static void main(String[] args) throws IOException {
    //     https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
    //     http://www.haoming8.cn/baobao/10881.html
    //     http://www.haoming8.cn/baobao/7641.html
        
        // 定义变量记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
        
        // 爬取数据,把网上所有的数据拼接成一个字符串
        String s1 = webCrawler(familyNameNet);
        String s2 = webCrawler(boyNameNet);
        String s3 = webCrawler(girlNameNet);
        // 通过正则表达式把其中符合需求的数据读取出来
        ArrayList<String> familNameList = getData(s1, "([\\u4E00-\\u9FA5]{4})(，|。)", 1);
        ArrayList<String> boyNameList = getData(s2, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
        ArrayList<String> girlNameList = getData(s3, "(.. ){4}..", 0);
        // 处理数据
        ArrayList<String> nameList = new ArrayList<>();
        for (String str : familNameList) {
            for(int i = 0;i <str.length();i++){
                nameList.add(str.charAt(i) + "");
            }
        }
        
        ArrayList<String> boyList = new ArrayList<>();
        for (String str : boyNameList) {
            if(!boyList.contains(str)){
                boyList.add(str);
            }
        }
        ArrayList<String> girlList = new ArrayList<>();
        for (String str : girlNameList) {
            String[] arr = str.split(" ");
            for (String s : arr) {
                girlList.add(s);
            }
        }
        
        ArrayList<String> info = getInfo(nameList, boyList, girlList, 10, 10);
        System.out.println(info);
    }
    
    /*
    *
    * */
   public static ArrayList<String> getInfo(ArrayList<String> nameList,ArrayList<String> boyList,ArrayList<String> girlList,int boysize,int girlsize){
       HashSet<String> boys = new HashSet();
       while (true) {
           if(boys.size() == boysize){
               break;
           }
           Collections.shuffle(nameList);
           Collections.shuffle(boyList);
           boys.add(nameList.get(0) + boyList.get(0));
       }
       HashSet<String> girls = new HashSet();
       while (true) {
           if(girls.size() == girlsize){
               break;
           }
           Collections.shuffle(nameList);
           Collections.shuffle(girlList);
           girls.add(nameList.get(0) + girlList.get(0));
       }
       ArrayList<String> peopleList =  new ArrayList<>();
       Random r = new Random();
       for (String boy : boys) {
           int age = r.nextInt(10) + 18;
           peopleList.add(boy + "-男-" + age);
       }
       for (String girl : girls) {
           int age = r.nextInt(8) + 18;
           peopleList.add(girl + "-女-" + age);
       }
       Collections.shuffle(peopleList);
       return peopleList;
   }
    
    
    /*
    * 根据正则表达式返回获取的数据
    * 参数一 完整的字符串
    * 参数二 正则表达式
    * 返回值 真正想要的数据
    * */
    public static ArrayList<String> getData(String names,String regex,int index){
        ArrayList<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(names);
        while (matcher.find()) {
            String group = matcher.group(index);
            // System.out.println(group);
            list.add(group);
        }
        return list;
    }
    
    /*
    * 从网络上爬取数据把数据拼接成字符串返回
    *
    * 形参：网址
    * 返回值：爬取到的所有数据
    * */
    public static String webCrawler(String net) throws IOException {
        // 定义stringbuilder拼接数据
        StringBuilder sb = new StringBuilder();
        // 创建一个网址对象
        URL url = new URL(net);
        // 拼接上这个网址
        // 保证网络畅通且网址可以访问
        URLConnection con = url.openConnection();
        // 读取数据
        InputStreamReader isr = new InputStreamReader(con.getInputStream());
        int ch;
        while ((ch = isr.read()) != -1){
            sb.append((char)ch);
        }
        // 释放资源
        isr.close();
        // 数据返回
        return sb.toString();
    }
}
